-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for serializing json api resource collections into root key #23
Conversation
Hold off on merging this. I'm formulating some additional thoughts. |
I'm not thrilled with using the context hash this way, but I can't come up with anything better. Have any ideas? If you like it as is feel free to merge. |
The only other thing I can think of is writing a new method on the json_api adapter called "Collection" instead of using entities for this. I'm not sure if that is any better. |
I think this brings us back to this discussion. See my proposal there re. a new I would try to avoid overloading the behaviour of the DSL by providing hash options and instead try to come up with a DSL that expresses the most common use cases across media types. One useful distinction can be the following:
So going back to my previous proposal I'd opt for extending the DSL with collection :users, item, UserSerializer
entities :accounts, item.accounts, AccountSerializer The default implementation of
Also note that, in JsonAPI, items in the main collection can have references to objects in the collection :users, item do |user, user_serializer|
user_serializer.name user.name
# this adds the account entity to the top "linked" object
user_serializer.reference :account, user.account, AccountSerializer
# this embeds the entity into the current object, as normal
user_serializer.entity :company, user.company, CompanySerializer
end The |
Sounds great. I'll work on these changes.
|
Okay. I've made the changes using collection. I don't think it is ever valid to have an entity embedded in the current object instead of being added to the [:linked] objects, so calling entity or entities within the collection will always add them to :linked. I can clean all this up significantly once serializer_from_block_or_class returns a serializer object instead of a hash, specifically so I have access to the root_name of the entities. |
@@ -55,12 +55,27 @@ def entities(name, collection, serializer_class = nil, context_options = {}, &bl | |||
end | |||
end | |||
|
|||
def collection(name, collection, serializer_class = nil, context_options = {}, &block) | |||
@treat_as_resource_collection = true | |||
data[:resource_collection] = [] unless data[:resource_collection].is_a?(Array) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just
data[:resource_collection] ||= []
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to
Adapter#initalize
#adapter.rb
def initialize(serializer)
@serializer = serializer
@data = Hash.new{|h,k| h[k] = {}}
end
data[:resource_collection]
will be a hash the first time it is accessed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. And that's my own code :)
I'd love to uses these changes in the project I'm working on. Are there any further changes you'd like me to make, or is this good to go now? |
Good to go as it makes the JsonAPI functional. Will merge and release in a minute. Are we still extracting JsonAPI into a separate gem at some point or do you prefer to wait until the serializer_from_block_or_class issue is sorted? |
Allow for serializing json api resource collections into root key
I prefer to wait until serializer_from_block_or_class is sorted, since it is a significant api change. |
How do you feel about this schema for json api resource collections?